class MessagesController < ApplicationController
  before_filter :require_user_lib, :only => :index

  # GET /messages
  # GET /messages.xml
  def index
    #@messages = Message.find(:all, :conditions => "receiver = '#{session[:user_id]}'")
    @messages = Message.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @messages }
    end
  end

  def in_get_by_uid
    @messages = Message.find(:all, :conditions => "receiver = '#{params[:id]}'")
    render "index"
  end

  def out_get_by_uid
    @messages = Message.find(:all, :conditions => "sender = '#{params[:id]}'")
    render "index"
  end

  def get_by_brid
    @messages = Message.find(:all, :conditions => "borrow_rec_id = '#{params[:id]}'")
    render "index"
  end

  # GET /messages/1
  # GET /messages/1.xml
  def show
    @message = Message.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @message }
    end
  end

  # GET /messages/new
  # GET /messages/new.xml
  def new
    @message = Message.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @message }
    end
  end

  # GET /messages/1/edit
  def edit
    @message = Message.find(params[:id])
  end

  # POST /messages
  # POST /messages.xml
  def create
    @message = Message.new(params[:message])

    respond_to do |format|
      if @message.save
        flash[:notice] = 'Message was successfully created.'
        format.html { redirect_to(@message) }
        format.xml  { render :xml => @message, :status => :created, :location => @message }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @message.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /messages/1
  # PUT /messages/1.xml
  def update
    @message = Message.find(params[:id])

    respond_to do |format|
      if @message.update_attributes(params[:message])
        flash[:notice] = 'Message was successfully updated.'
        format.html { redirect_to(@message) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @message.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /messages/1
  # DELETE /messages/1.xml
  def destroy
    @message = Message.find(params[:id])
    @message.destroy

    respond_to do |format|
      format.html { redirect_to(messages_url) }
      format.xml  { head :ok }
    end
  end

  # the lib can ship the book with this method
  def deliver
    # create DELIVER message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      br = BorrowRec.find(@message.borrow_rec_id)
      br.status = 'delivered'
      br.save
      deliver_message = Message.new(
        :mtype => "DELIVER",
        :mtime => Time.new,
        :handled => false,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => br.id
      )
      deliver_message.save
      flash[:notice] = 'Deliver message was created!'
    end
    redirect_to request.referer
  end

  # DECLINE for REQUEST message
  def decline_request
    # create DECLINE_REQUEST message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      br = BorrowRec.find(@message.borrow_rec_id)
      br.status = 'declined'
      br.save
      decline_message = Message.new(
        :mtype => "DECLINE_REQUEST",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => br.id
      )
      decline_message.save
      flash[:notice] = 'Decline_Request message was created!'
    end
    redirect_to request.referer

  end

  # after deliver, the user indicates that he has received the book
  def confirm
    # create CONFIRM message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      br = BorrowRec.find(@message.borrow_rec_id)
      br.status = 'confirmed'
      # you can borrow the book for one month
      br.start_date = Time.new
      br.due_date = br.start_date + (60*60*24*30)
      br.save
      confirm_message = Message.new(
        :mtype => "CONFIRM",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => br.id
      )
      confirm_message.save
      flash[:notice] = 'Confirm message was created!'
    end
    redirect_to request.referer
  end

  # ACCEPT for RESERVE message
  def accept_reserve
    # create ACCEPT_RESERVE message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      br = BorrowRec.find(@message.borrow_rec_id)
      br.status = 'waiting'
      br.save
      accept_message = Message.new(
        :mtype => "ACCEPT_RESERVE",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => br.id
      )
      accept_message.save
      holder_br = BorrowRec.find(:first, :conditions => "book_id = #{br.book_id} and (status = 'requested' or status = 'delivered' or status = 'confirmed')")
      notify_message = Message.new(
        :mtype => "NOTIFY",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => holder_br.user_id,
        :borrow_rec_id => holder_br.id
      )
      notify_message.save
      flash[:notice] = 'Accept_Reserve message was created!'
    end
    redirect_to request.referer
  end

  # DECLINE for RESERVE message
  def decline_reserve
    # reserve DECLINE_RESERVE message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      br = BorrowRec.find(@message.borrow_rec_id)
      br.status = 'declined'
      br.save
      decline_message = Message.new(
        :mtype => "DECLINE_RESERVE",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => br.id
      )
      decline_message.save
      flash[:notice] = 'Decline_Reserve message was created!'
    end
    redirect_to request.referer

  end

  # ACCEPT for RENEW message
  def accept_renew
    # create ACCEPT_RENEW message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      br = BorrowRec.find(@message.borrow_rec_id)
      # extend the date for another month
      br.due_date = br.due_date + (60*60*24*30)
      br.save
      accept_message = Message.new(
        :mtype => "ACCEPT_RENEW",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => br.id
      )
      accept_message.save
      flash[:notice] = 'Accept_Renew message was created!'
    end
    redirect_to request.referer
  end

  # DECLINE for RENEW message
  def decline_renew
    # create DECLINE_RENEW message
    @message = Message.find(params[:id])
    if @message.handled == false
      @message.handled = true
      @message.save
      decline_message = Message.new(
        :mtype => "DECLINE_RENEW",
        :mtime => Time.new,
        :handled => true,
        :sender => @message.receiver,
        :receiver => @message.sender,
        :borrow_rec_id => @message.borrow_rec_id
      )
      decline_message.save
      flash[:notice] = 'Decline_Renew message was created!'
    end
    redirect_to request.referer
  end

end
